﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KhaoSatSinhVienTotNghiep.Model
{
   public partial class PhieuKhaoSat
    {
      private static KhaoSatSVTNDataContext context = new KhaoSatSVTNDataContext();
       public static List<PhieuKhaoSat> GetAll()
       {
           return context.PhieuKhaoSats.ToList();
       }
       public static List<PhieuKhaoSatDetails> GetByIDDotKS(int idDotKS)
       {
           var result = context.PhieuKhaoSats.Join(context.DotKhaoSats, pks => pks.MaDotKS, dks => dks.MaDotKS, (pks, dks) => new
           {
               MaDotKS = dks.MaDotKS,
               MaCH = pks.MaCauHoi
           }).Join(context.CauHois, a => a.MaCH, ch => ch.MaCauHoi, (a, ch) => new
           {
               MaDotKS = a.MaDotKS,
               MaCH = a.MaCH,
               ChiTietCH = ch.ChiTiet
           }).Join(context.CauTLs, b => b.MaCH, ctl => ctl.MaCauHoi, (b, ctl) => new PhieuKhaoSatDetails
           {
               MaDotKS=b.MaDotKS,
               MaCauHoi=b.MaCH,
               MaCauTL=ctl.MaCauTL,
               ChiTietCH=b.ChiTietCH,
               ChiTietCTL=ctl.ChiTiet
           }).Where(pksDT=>pksDT.MaDotKS==idDotKS);
           return result.ToList();
       }
       public static bool Add(int maCH, int maDotKS)
       {
           if (CheckExist(maCH, maDotKS) == true)
               return false;
           else
           {
               PhieuKhaoSat pks = new PhieuKhaoSat
               {
                   MaCauHoi = maCH,
                   MaDotKS = maDotKS
               };
               context.PhieuKhaoSats.InsertOnSubmit(pks);
               context.SubmitChanges();
               return true;
           }
       }

       public static bool CheckExist(int maCH, int maDotKS)
       {
           var result = context.PhieuKhaoSats.Where(pks => pks.MaCauHoi == maCH && pks.MaDotKS == maDotKS).FirstOrDefault();
           if (result != null)
               return true;
           return false;

       }
    }
}
